/**
 * 地址信息增删改查
 */
import { addAddress, deleteAddress, editAddress, getAddressList, fetchStoreAddress } from '@/api/address'


interface Params {
  pageNumber: number
  pageSize: number
  [key: string]: any
}

let exportDefaultValue = {
  id: 'id',
  fullAddress: 'detail',
  addressDetail: "consigneeAddressPath",
  addressName: 'name',
  phone: 'mobile',
  defaultAddress: 'isDefault',
}
class Address {
  public params: any
  public data: any

  constructor(params: any) {
    // 获取地址列表

    this.params = params
    this.data = params
  }

  fetch(): Promise<any> {
    return new Promise((resolve, reject) => {
      getAddressList(this.data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  }
  fetchStoreList(storeId:string): Promise<any> {
    console.log(storeId,this.params)
    return new Promise((resolve, reject) => {
      fetchStoreAddress(storeId,this.data).then((res) => {
        console.log(exportDefaultValue)
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  }

  delete(id: number | string): Promise<any> {
    return new Promise((resolve, reject) => {
      deleteAddress(id).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  }

  create(data: any): Promise<any> {
    return new Promise((resolve, reject) => {
      addAddress(data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  }

  update(data: any): Promise<any> {
    return new Promise((resolve, reject) => {
      editAddress(data).then((res) => {
        resolve(res)
      }).catch((err) => {
        reject(err)
      })
    })
  }




  // 自定义 key 值设置映射关系
  defaultOptions = {
    id: 'id',
    fullAddress: 'detail',
    addressDetail: "consigneeAddressPath",
    addressName: 'name',
    phone: 'mobile',
    defaultAddress: 'isDefault',
  }

  defaultStoreOptions = {
    id: 'id',
    fullAddress: 'address',
    addressDetail: "address",
    addressName: 'addressName',
    phone: 'mobile',
    defaultAddress: 'isDefault',
  }
}
export default Address
